package com.contain.minio.properties;

import com.contain.minio.config.ClassPathMinioScanner;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;

import java.util.Properties;

/**
 * @Auther: tianchao
 * @Date: 2021/8/19 22:00
 * @Description:
 */
public class FaceScannerRegister implements ImportBeanDefinitionRegistrar, EnvironmentAware {
    private Environment environment;
    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        ClassPathMinioScanner scanner = new ClassPathMinioScanner(registry);
        ConfigurableEnvironment c = (ConfigurableEnvironment) environment;
        MutablePropertySources m = c.getPropertySources();
        Properties p = new Properties();
        for (FaceConstant fc : FaceConstant.values()) {
            String val = environment.getProperty(fc.getKey());
            if (val == null || "".equals(val)) {
                p.put(fc.getKey(), fc.getDefaultValue());
            }
        }
        if (StringUtils.isEmpty(environment.getProperty("minio.isopen"))){
            p.put("minio.isopen", "false");
        }
        m.addFirst(new PropertiesPropertySource("defaultProperties", p));
        scanner.doScan("com.contain.minio");

    }
}
